package fzu.functiongather.Select;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import fzu.functiongather.R;
import fzu.functiongather.Utils.wheelview.adapter.ArrayWheelAdapter;
import fzu.functiongather.Utils.wheelview.widget.WheelView;

/**
 * Created by Administrator on 2016/8/20.
 */
public class SelectTwoActivity extends Activity implements View.OnClickListener {
    private TextView et_authentication_selectedBankID;
    private TextView v_tv_sma_bankName;
    private LinearLayout v_authentication_selecteBank;
    private Button v_account_security_auth_bankplace_cancel;
    private Button v_account_security_auth_bankplace_ok;
    private AlertDialog dialogShow;
    private WheelView ProvWheelView, CityWheelView;

    private Map<String,String> proviceMap = new HashMap<>();
    private Map<String, HashMap<String, String>> proCityMap=new HashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_main);
        initView();
    }

    private void initView(){
        et_authentication_selectedBankID = (TextView) findViewById(R.id.et_authentication_selectedBankID);
        v_tv_sma_bankName = (TextView) findViewById(R.id.v_tv_sma_bankName);
        v_authentication_selecteBank = (LinearLayout) findViewById(R.id.v_authentication_selecteBank);
        v_authentication_selecteBank.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.v_authentication_selecteBank:
                proviceselectDialog();
                break;
            case R.id.v_account_security_auth_bankplace_ok:
                String provice = (String)ProvWheelView.getSelectionItem();
                String city = (String)CityWheelView.getSelectionItem();
                v_tv_sma_bankName.setText(provice + "  " + city);
                v_tv_sma_bankName.setTextColor(getResources().getColor(R.color.text_black));
                dialogShow.dismiss();
                break;
            case R.id.v_account_security_auth_bankplace_cancel:
                dialogShow.dismiss();
                break;
        }
    }

    private void proviceselectDialog() {
        View dialog =  getLayoutInflater().inflate(R.layout.v_view_dialog_provice_select, null);
        v_account_security_auth_bankplace_cancel =  (Button)dialog.findViewById(R.id.v_account_security_auth_bankplace_cancel);
        v_account_security_auth_bankplace_ok =  (Button)dialog.findViewById(R.id.v_account_security_auth_bankplace_ok);
        v_account_security_auth_bankplace_cancel.setOnClickListener(this);
        v_account_security_auth_bankplace_ok.setOnClickListener(this);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.create();
        dialogShow = builder.show();
        dialogShow.getWindow().setContentView(dialog);

        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogShow.getWindow().getAttributes(); // 获取对话框当前的参数值
//        p.height = (int) (d.getHeight() * 0.5); // 高度设置为屏幕的0.6
        p.width = (int) (d.getWidth() * 0.9); // 宽度设置为屏幕的0.65
        dialogShow.getWindow().setAttributes(p);
        initProvAndCity(dialog);
        createProviceDatas();
    }

    /**
     * 联动WheelView初始化
     */
    private void initProvAndCity(View view) {

        ProvWheelView = (WheelView) view.findViewById(R.id.ProvWheelView);
        ProvWheelView.setWheelAdapter(new ArrayWheelAdapter(this));
        ProvWheelView.setSkin(WheelView.Skin.Holo);
        ProvWheelView.setWheelData(createMainDatas1());
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextSize = 20;
        style.textSize = 16;
        ProvWheelView.setStyle(style);

        CityWheelView = (WheelView) view.findViewById(R.id.CityWheelView);
        CityWheelView.setWheelAdapter(new ArrayWheelAdapter(this));
        CityWheelView.setSkin(WheelView.Skin.Holo);
        CityWheelView.setWheelData(createMainDatas1());

        CityWheelView.setStyle(style);


    }
    public void createProviceDatas() {



        ArrayList<String> provincenamelist = new ArrayList<String>();

        provincenamelist.add(0, "北京");
        provincenamelist.add(1, "天津");
        provincenamelist.add(2, "福建");

        proviceMap.clear();
        proviceMap.put("100", "北京");
        proviceMap.put("101", "天津");
        proviceMap.put("102", "福建");


        ProvWheelView.setWheelData(provincenamelist);
        ProvWheelView.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
            @Override
            public void onItemSelected(int position, Object o) {
                Log.d("setOnWheelItemSelected", "position=" + position + " --  " + o.toString());
                Log.d("setOnWheelItemSelected", "position=" + ProvWheelView.getCurrentPosition() + "  " + ProvWheelView.getSelectionItem().toString());
                Log.d("setOnWheelItemSelected", "getItemAtPosition=" + ProvWheelView.getItemAtPosition(position).toString());
                String bankProId = "";
                String proviceName = "";

                for (String provinceID : proviceMap.keySet()) {
                    if (proviceMap.get(provinceID) == o.toString()) {
                        bankProId = provinceID;
                        proviceName = proviceMap.get(provinceID);
                        break;
                    }
                }
                createCityDatas(bankProId, proviceName);
            }
        });

        ProvWheelView.deferNotifyDataSetChanged();


}

    private void createCityDatas(final String provinceID,final String proviceName) {


                proCityMap.clear();//清除旧数据
                List<String> cityNameList = new ArrayList<>();
                HashMap<String,String>  cityNameMap = new HashMap<>();
                final HashMap<String,List<String>>  proCityList = new HashMap<>();
        if(provinceID.equals("100")){
            cityNameMap.put("1000", "北京");
            cityNameList.add(0, "北京");
        }else if(provinceID.equals("101")){
            cityNameMap.put("1010","天津");
            cityNameList.add(0,"天津");
        }else if(provinceID.equals("102")){
            cityNameMap.put("1020","福州");
            cityNameMap.put("1020","厦门");
            cityNameMap.put("1020","泉州");
            cityNameMap.put("1020", "南平");
            cityNameList.add(0, "福州");
            cityNameList.add(1,"厦门");
            cityNameList.add(2,"泉州");
            cityNameList.add(3,"南平");
        }


        CityWheelView.setWheelData(cityNameList);// List<城市名称>
        proCityList.put(proviceName, cityNameList);//HashMap<省份, List<城市名称>>
        proCityMap.put(proviceName, cityNameMap);//HashMap<省份, HashMap<城市id,城市名称>>

                    CityWheelView.deferNotifyDataSetChanged();
                    CityWheelView.setOnWheelItemSelectedListener(new WheelView.OnWheelItemSelectedListener() {
                        @Override
                        public void onItemSelected(int position, Object o) {
                            Log.d("setOnWheelItemSelected", "position=" + position + " --  " + o.toString());
                            Log.d("setOnWheelItemSelected", "position=" + CityWheelView.getCurrentPosition() + "  " + CityWheelView.getSelectionItem().toString());
                            Log.d("setOnWheelItemSelected", "getItemAtPosition=" + CityWheelView.getItemAtPosition(position).toString());
                            String cityID = "";
                            String cityName = "";

                            for (String proName : proCityMap.keySet()) {
                                HashMap<String, String> temp = proCityMap.get(proName);
                                for (String id : temp.keySet()) {
                                    if (temp.get(id) == o.toString()) {
                                        cityName = temp.get(id);
                                        cityID = id;
                                        break;
                                    }
                                }
                            }
                        }
                    });
                    Log.d("Security", "城市列表加载成功");



    }




    private List<String> createMainDatas1() {
        String[] strings = {"黑龙江", "吉林", "辽宁"};
        return Arrays.asList(strings);
    }
}
